package com.uni.sofia.reader.presentation
{
	import com.uni.sofia.reader.magazine.Magazine;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;

	/**
	 * Model class responsible of content of the magazine view
	 */ 
	public class MagazinePresentation extends EventDispatcher
	{
		private var _magazine:Magazine;
		
		/**
		 * The magazine
		 * 
		 * @see Magazine
		 */ 
		public function set magazine(value:Magazine):void
		{
			_magazine = value;
			currentPage = _magazine.coverPage.image;
			index = _magazine.coverPage.pageNumber;
			pages = _magazine.pages;
			selectPage(0);
			dispatchEvent(new Event("magazineSet"));
        }
		
		[Bindable(Event='magazineSet')]
		public function get magazine():Magazine
		{
			return _magazine;
		} 
		
		/**
		 * Sets the selection of a page
		 * @param index page number to be selected
		 */ 
		public function selectPage(index:int):void
		{
			this.index = index;
			
			if(index > 0)
				previousPage = magazine.getPage(index - 1).image;
			else previousPage = null;
			
			currentPage	= magazine.getPage(index).image;
			
			if(index < magazine.size - 1){
				nextPage = magazine.getPage(index + 1).image;
			}
			else nextPage = null;
		}
		
		[Bindable]
		/**
		 * The path to the previous page(if exists)
		 */ 
		public var previousPage:String;
		
		[Bindable]
		/**
		 * The path to the current selected page
		 */ 
		public var currentPage:String;
		
		[Bindable]
		/**
		 * The path to the newxt page(if exists)
		 */ 
		public var nextPage:String;
		
		[Bindable]
		/**
		 *the pages of the magazine 
		 */
		public var pages:ArrayCollection; 
		
		[Bindable]
		/**
		 * Selected page number
		 */ 
		public var index:int; 
	}
}